package com.jk.service.impl;

import com.jk.entity.Company;
import com.jk.entity.Message;
import com.jk.entity.Register;
import com.jk.entity.ResponseMessage;
import com.jk.mapper.LoginMapper;
import com.jk.service.LoginService;
import com.jk.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 荣楠楠
 * 创建时间：2018/4/11 22:12
 * @Description: TODO
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private LoginMapper loginMapper;

   @Resource
   private UserService userService;

    /* *
     * @Description: 用户登录  判断用户名是否存在，密码是否正确
     * @author rnn
     * @date 2018/4/16 14:40
     * @param [register]
     * @return com.jk.entity.Message
     */
    @Override
    public Message Login(Register register) {

       Register login = loginMapper.queryregister(register.getRegisterName());

        Message message = new Message();

        //如果用户名存在
        if (login != null) {

            if(login.getRegisterPass().equals(register.getRegisterPass())){

              message.setMid(00);
              message.setUsername(register.getRegisterName());
              message.setMessage("登录成功");
              message.setUserid(login.getRegisterId());
              String  cookiewy ="596587411222";
              ResponseMessage   messages  = userService.queryShopbycookie(cookiewy,login.getRegisterId());
              return message;

            // return "登录成功";

             }else {

                message.setMid(02);

                message.setMessage("密码错误");

                return message;


               //return  "密码错误";
                }

        }else {

            message.setMid(01);

            message.setMessage("用户名不存在");

            return message;
         //  return  "用户名不存在";

        }


    }

    /* *
     * @Description:个人用户注册  判断用户名是会否已存在，密码必须为6-16位字母或数字
     * @author rnn
     * @date 2018/4/16 14:51
     * @param [register]
     * @return com.jk.entity.Message
     */
    @Override
    public Message saveRegister(Register register) {
        //查询用户名是否存在
        Register reg = loginMapper.queryregister(register.getRegisterName());

        Message message = new Message();

        //密码正则   必须为6-16位字母或数字
        String regularExpression = "(^[a-zA-Z\\d]{6,16}$)";

        boolean matches = register.getRegisterPass().matches(regularExpression);

        //如果用户名不为空
        if (register.getRegisterName()!=""&&!"".equals(register.getRegisterName())) {

        if (reg!=null){

            message.setMid(01);

            message.setMessage("用户名已存在");

            return message;

        }else if (!matches){

            message.setMid(02);

            message.setMessage("密码格式不正确  6-16位字母或数字");

            return message;

        } else{

            loginMapper.saveRegister(register);

            message.setMid(00);

            message.setMessage("注册成功");

            return message;

          }

        }else{

            message.setMid(03);

            message.setMessage("用户名不能为空");

            return message;

        }
    }



    /* *
     * @Description: 企业登录 根据手机号查询
     * @author rnn
     * @date 2018/4/16 14:55
     * @param [companyPersonPhone]
     * @return com.jk.entity.Company
     */
    @Override
    public Company queryCompanyPersonPhone(String companyPersonPhone) {


        return  loginMapper.queryCompanyPersonPhone(companyPersonPhone);
    }

    /* *
     * @Description: 通过手机号，上传证件图片企业注册
     * @author rnn
     * @date 2018/4/16 19:02
     * @param [company]
     * @return com.jk.entity.Message
     */
    @Override
    public String regCompany(Company company) {

        //查询手机号是否已存在
        Company cc = loginMapper.queryCompany(company.getCompanyPersonPhone());

        Message message = new Message();

        //手机号正则
            String regularExpression = "(^1[3|4|5|7|8]\\d{9}$)";

            boolean matche =company.getCompanyPersonPhone().matches(regularExpression);

            //如果手机号不为空
            if (company.getCompanyPersonPhone()!=""&&!"".equals(company.getCompanyPersonPhone())) {

            if (cc != null) {

               /* message.setMid(01);

                message.setMessage("手机号已存在");

                return message;*/
               return "手机号已存在";

            }else if (!matche){

               /* message.setMid(04);

                message.setMessage("手机号格式不正确");

                return message;*/
               return  "手机号格式不正确";

            } else {

                loginMapper.saveCompany(company);

                /*message.setMid(00);

                message.setMessage("注册成功");

                return message;*/

                return "注册成功";

            }


        }else{

              /*  message.setMid(02);

                message.setMessage("手机号不能为空");

                return message;
*/
              return "手机号不能为空";

        }

    }




}
